package com.example.restatr.Atcivity.Main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.restatr.Atcivity.Bus.Bean.BusOrderBean;
import com.example.restatr.Atcivity.TakeOut.TakeOutOrderAt;
import com.example.restatr.Bean.OrderBean;
import com.example.restatr.Atcivity.TakeOut.Bean.TakeOutOrderInfoBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class OrderListAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private LinearLayout Null;
    private GridView gridView;
    private ListView DdList;
    private TextView lx_text;
    private List<String> orderTypeList = new ArrayList<>();
    private List<OrderBean.RowsBean> rowsBeans = new ArrayList<>();
    private String orderType = "";
    public static TakeOutOrderInfoBean takeOutOrderInfoBean = new TakeOutOrderInfoBean();
    private Intent intent;
    private boolean[] isSelect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_ddlb );

        tb = findViewById( R.id.toolbar );
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        Null = findViewById( R.id.Null );
        gridView = findViewById( R.id.gridView );
        DdList = findViewById( R.id.DdList );
        lx_text = findViewById( R.id.lx_text );
        search.setVisibility( View.GONE );
        Collections.addAll( orderTypeList, "全部", "外卖订单", "电影订单", "巴士订单" );
        isSelect = new boolean[orderTypeList.size()];
        isSelect[0] = true;
        gridView.setAdapter( new gridViewAdapter() );
        Tools.SetBarTitle( tb, barTitle, "订单列表", search, OrderListAt.this, false );
        intent = new Intent( OrderListAt.this, TakeOutOrderAt.class );
    }


    //设置订单图标
    private void SetImg(String type, ImageView img) {
        switch (type) {
            case "外卖订餐":
                img.setImageResource( R.drawable.wm );
                break;
            case "看电影":
                img.setImageResource( R.drawable.dy );
                break;
            case "智慧巴士":
                img.setImageResource( R.drawable.bus );
                break;
        }
    }

    //获取订单类型、获取订单
    private void GetType() {
        String s = lx_text.getText().toString();
        DdList.setVisibility( View.GONE );
        switch (s) {
            case "全部":
                SetListDisPlay( rowsBeans );
                break;
            case "外卖订单":
                List<OrderBean.RowsBean> 外卖订餐 = rowsBeans.stream().filter( new Predicate<OrderBean.RowsBean>() {
                    @Override
                    public boolean test(OrderBean.RowsBean rowsBean) {
                        return rowsBean.getOrderTypeName().equals( "外卖订餐" );
                    }
                } ).collect( Collectors.toList() );
                SetListDisPlay( 外卖订餐 );
                break;
            case "电影订单":
                List<OrderBean.RowsBean> 看电影 = rowsBeans.stream().filter( new Predicate<OrderBean.RowsBean>() {
                    @Override
                    public boolean test(OrderBean.RowsBean rowsBean) {
                        return rowsBean.getOrderTypeName().equals( "看电影" );
                    }
                } ).collect( Collectors.toList() );
                SetListDisPlay( 看电影 );
                break;
            case "巴士订单":
                List<OrderBean.RowsBean> 智慧巴士 = rowsBeans.stream().filter( new Predicate<OrderBean.RowsBean>() {
                    @Override
                    public boolean test(OrderBean.RowsBean rowsBean) {
                        return rowsBean.getOrderTypeName().equals( "智慧巴士" );
                    }
                } ).collect( Collectors.toList() );
                SetListDisPlay( 智慧巴士 );
                break;
        }
    }

    //判断订单状态
    private String isOrderStatus(int status) {
        String type = null;
        if (status == 0) {
            type = "待支付";
        } else {
            type = "完成";
        }
        return type;
    }

    //获取订单
    private void GetOrderList() {
        new Thread( new Runnable() {
            @Override
            public void run() {
                String token = Tools.Get( "/prod-api/api/allorder/list", MainActivity.sp.getString( "token", null ) );
                OrderBean orderBean = new Gson().fromJson( token, OrderBean.class );
                //按照支付状态排序
                List<OrderBean.RowsBean> collect = orderBean.getRows().stream().sorted( Comparator.comparing( OrderBean.RowsBean::getOrderStatus ).reversed() ).collect( Collectors.toList() );
                //获取巴士订单
                String token2 = Tools.Get( "/prod-api/api/bus/order/list", MainActivity.sp.getString( "token", "" ) );
                BusOrderBean busOrderBean = new Gson().fromJson( token2, BusOrderBean.class );
                //按照创建时间排序
                List<BusOrderBean.RowsBean> collect2 = busOrderBean.getRows().stream().sorted( Comparator.comparing( BusOrderBean.RowsBean::getCreateTime ).reversed() ).collect( Collectors.toList() );
                rowsBeans.clear();
                rowsBeans.addAll( collect );
                for (int i = 0; i < collect2.size(); i++) {
                    rowsBeans.add( new OrderBean.RowsBean(
                            collect2.get( i ).getOrderNum(),
                            collect2.get( i ).getPrice(),
                            isOrderStatus( collect2.get( i ).getStatus() ),
                            String.valueOf( collect2.get( i ).getPayTime() ),
                            collect2.get( i ).getPath() + " " + collect2.get( i ).getStart() + "→" +
                                    collect2.get( i ).getEnd(),
                            "bus",
                            "智慧巴士" )
                    );
                }
                runOnUiThread( new Runnable() {
                    @Override
                    public void run() {
                        SetListDisPlay( rowsBeans );
                    }
                } );
            }
        } ).start();
    }

    //设置显示状态
    private void SetListDisPlay(List<OrderBean.RowsBean> rowsBeans) {
        if (rowsBeans.size() > 0) {
            Null.setVisibility( View.GONE );
            DdList.setVisibility( View.VISIBLE );
            DdList.setAdapter( new DdListAdapter( rowsBeans ) );
        } else {
            Null.setVisibility( View.VISIBLE );
            DdList.setVisibility( View.GONE );
        }
    }

    //获取订单详情
    private void GetDdInfo(String name, String OrderNo) {
        switch (name) {
            case "外卖订餐":
                new Thread( () -> {
                    String token = Tools.Get( "/prod-api/api/takeout/order/" + OrderNo, MainActivity.sp.getString( "token", "" ) );
                    takeOutOrderInfoBean = new Gson().fromJson( token, TakeOutOrderInfoBean.class );
                    Log.d( "TAG", "订单详情订单号: " + OrderNo );
                    intent.putExtra( "type", "ddlb" );
                    intent.putExtra( "pos", 1 );
                    intent.putExtra( "Djid", takeOutOrderInfoBean.getData().getSellerInfo().getId() );
                    intent.putExtra( "url", takeOutOrderInfoBean.getData().getSellerInfo().getImgUrl() );
                    intent.putExtra( "location", takeOutOrderInfoBean.getData().getSellerInfo().getAddress() );
                    intent.putExtra( "score", takeOutOrderInfoBean.getData().getSellerInfo().getScore() );
                    intent.putExtra( "name", takeOutOrderInfoBean.getData().getSellerInfo().getName() );
                    intent.putExtra( "OrderNo", OrderNo );
                    startActivity( intent );
                } ).start();
                break;
        }
    }

    //设置订单状态
    private void SetOrderStatus(String type, TextView phone, int position) {
        switch (type) {
            case "待支付":
            case "待付款":
                phone.setText( "待支付" );
                phone.setTextColor( Color.parseColor( "#FD815A" ) );
                break;
            case "待评价":
                phone.setText( "待评价" );
                phone.setTextColor( Color.parseColor( "#09F175" ) );
                break;
            case "完成":
            case "已付款":
                phone.setText( "交易时间：" + rowsBeans.get( position ).getPayTime() );
                break;
        }
    }

    public void OnClick(View view) {
        if (gridView.getVisibility() == View.VISIBLE) {
            gridView.setVisibility( View.GONE );
        } else {
            gridView.setVisibility( View.VISIBLE );
        }
    }

    //订单适配器
    class DdListAdapter extends BaseAdapter {
        private List<OrderBean.RowsBean> rowsBeans;

        public DdListAdapter(List<OrderBean.RowsBean> rowsBeans) {
            this.rowsBeans = rowsBeans;
        }

        @Override
        public int getCount() {
            return rowsBeans.size();
        }

        @Override
        public Object getItem(int position) {
            return rowsBeans.get( position );
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate( R.layout.zfz_list_item, null );
            ImageView tupian = convertView.findViewById( R.id.tupian );
            TextView name = convertView.findViewById( R.id.name );
            TextView diz = convertView.findViewById( R.id.diz );
            TextView phone = convertView.findViewById( R.id.phone );
            TextView jg = convertView.findViewById( R.id.jg );
            RelativeLayout ddInfo = convertView.findViewById( R.id.ddInfo );

            tupian.setScaleType( ImageView.ScaleType.FIT_CENTER );
            SetImg( rowsBeans.get( position ).getOrderTypeName(), tupian );
            name.setText( rowsBeans.get( position ).getName() );
            diz.setText( "订单号：" + rowsBeans.get( position ).getOrderNo() );
            jg.setText( "¥" + rowsBeans.get( position ).getAmount() );
            SetOrderStatus( rowsBeans.get( position ).getOrderStatus(), phone, position );
            ddInfo.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    intent.putExtra( "orderStatus", rowsBeans.get( position ).getOrderStatus() );
                    GetDdInfo( rowsBeans.get( position ).getOrderTypeName(), rowsBeans.get( position ).getOrderNo() );
                    Log.d( "TAG", "所点击的订单号: " + rowsBeans.get( position ).getOrderNo() );
                }
            } );
            return convertView;
        }
    }

    //订单类型适配器
    class gridViewAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return orderTypeList.size();
        }

        @Override
        public Object getItem(int position) {
            return orderTypeList.get( position );
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate( R.layout.order_type_item, null );
            TextView text = convertView.findViewById( R.id.text );
            text.setText( orderTypeList.get( position ) );
            convertView.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < isSelect.length; i++) {
                        isSelect[i] = false;
                    }
                    isSelect[position] = true;
                    gridView.setAdapter( new gridViewAdapter() );
                    lx_text.setText( orderTypeList.get( position ) );
                    GetType();
                }
            } );
            text.setSelected( isSelect[position] );
            return convertView;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        GetOrderList();
    }
}